package org.example.handler;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.example.Constant.RabbitConstant;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

import java.io.IOException;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author : zhangbo
 * @data : 2024/7/12
 * @description :
 */
@Slf4j
public class MsgListener extends BaseListener {
    /**
     * 处理类
     */
    private ConcurrentHashMap<String, MessageHandler> handlers = new ConcurrentHashMap<>();

    /**
     * 注册处理类
     *
     * @param messageKey
     * @param messageHandler
     */
    public void registerMessageHandler(String messageKey, MessageHandler messageHandler) {
        handlers.put(messageKey, messageHandler);
    }

    //    @AsynLogMdcAnnotation   自定义注解
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitConstant.Queue.QUEUE_USER, durable = "true"),
            exchange = @Exchange(value = RabbitConstant.Exchange.DIRECT, type = ExchangeTypes.DIRECT, ignoreDeclarationExceptions = "true", durable = "true"),
            key = RabbitConstant.RoutingKey.ROUTE_USER))
    public void newUserListener(Message message, Channel channel) throws IOException {
        String registerMessageName = (String) message.getMessageProperties().getHeaders().get("MessageQueueName");
        MessageHandler messageHandler = handlers.get(registerMessageName);
        String logName = "根据手机号码修改状态";
        consumer(message, channel, messageHandler, logName, RabbitConstant.Queue.QUEUE_USER, RabbitConstant.RoutingKey.ROUTE_USER);
    }

    //    @AsynLogMdcAnnotation   自定义注解
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitConstant.Queue.QUEUE_FIND_ALL, durable = "true"),
            exchange = @Exchange(value = RabbitConstant.Exchange.DIRECT, type = ExchangeTypes.DIRECT, ignoreDeclarationExceptions = "true", durable = "true"),
            key = RabbitConstant.RoutingKey.ROUTE_FIND_ALL))
    public void findAllListener(Message message, Channel channel) throws IOException {
        String registerMessageName = (String) message.getMessageProperties().getHeaders().get("MessageQueueName");
        MessageHandler messageHandler = handlers.get(registerMessageName);
        String logName = "根据id批量修改状态";
        consumer(message, channel, messageHandler, logName, RabbitConstant.Queue.QUEUE_FIND_ALL, RabbitConstant.RoutingKey.ROUTE_FIND_ALL);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = RabbitConstant.Queue.QUEUE_UPDATE_STATUS, durable = "true"),
            exchange = @Exchange(value = RabbitConstant.Exchange.DIRECT, type = ExchangeTypes.DIRECT, ignoreDeclarationExceptions = "true", durable = "true"),
            key = RabbitConstant.RoutingKey.ROUTE_UPDATE_STATUS))
    public void updateStatusListener(Message message, Channel channel) throws IOException {
        String registerMessageName = (String) message.getMessageProperties().getHeaders().get("MessageQueueName");
        MessageHandler messageHandler = handlers.get(registerMessageName);
        String logName = "根据loginTime批量修改状态";
        updateStatus(message, channel, messageHandler, logName, RabbitConstant.Queue.QUEUE_UPDATE_STATUS, RabbitConstant.RoutingKey.ROUTE_UPDATE_STATUS);
    }
}

